package cn.iocoder.springboot.lab17.dynamicdatasource.controller;

import cn.iocoder.springboot.lab17.dynamicdatasource.mapper.LocalMapper;
import cn.iocoder.springboot.lab17.dynamicdatasource.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class TestController {

    @Resource
    LocalMapper localMapper;
    @Resource
    private CommonService commonService;

    @RequestMapping("/getById/{userId}")
    public Object getById(@PathVariable String userId) {
        return localMapper.getByUserId(userId);
    }

    @RequestMapping("/test/{projectId}")
    public Object test(@PathVariable String projectId) {
        try {
            commonService.syncSystem(projectId);
            commonService.syncEquipment(projectId);
        } catch (Exception e) {
            log.error("发生错误", e);
            commonService.deleteSystemAndObjectParam(projectId);
            return e.getMessage();
        }
        return "success";
    }

    @RequestMapping("/test2/{projectId}/{wdCode}/{code}")
    public Object test2(@PathVariable String projectId, @PathVariable String wdCode, @PathVariable String code) {
        try {
            commonService.syncInfosParam(projectId, wdCode, code);
        } catch (Exception e) {
            log.error("发生错误", e);
            return e.getMessage();
        }
        return "success";
    }
}
